﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BDH.PRE.Form
{
    public partial class FrmInput : DevExpress.XtraEditors.XtraForm
    {
        protected DataTable _dtb = new DataTable("Tmp");
        protected bool IsAdd { set; get; }
        protected bool IsEdit { set; get; }
        protected BLL.BaseBLL _bllBase = new BLL.BaseBLL();

        public FrmInput()
        {
            InitializeComponent();
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            ChangeStatus();
            ReadOnlyControl();
            PerformRefresh();
        }

        protected void ChangeStatus(bool Istrue = true)
        {
            btnAdd.Enabled = Istrue;
            btnEdit.Enabled = Istrue;
            btnDelete.Enabled = Istrue;
            btnRefresh.Enabled = Istrue;
            btnClose.Enabled = Istrue;

            btnSave.Enabled = !Istrue;
            btnCancel.Enabled = !Istrue;
        }

        public string Proper(string name)
        {
            try
            {
                string s = name.Trim();
                string result = "";

                string tmp = ""; if (s != "") tmp = s.Substring(0, 1);
                result += tmp.ToUpper();
                for (int i = 1; i < s.Length; i++)
                {
                    if ((s[i - 1].ToString() == " ") && (s[i].ToString() != " "))
                    {
                        string ss = s[i].ToString();
                        result += ss.ToUpper();
                    }
                    else
                    {
                        string ss = s[i].ToString();
                        result += ss.ToLower();

                    }
                }

                return result;
            }
            catch (Exception) { return null; }
        }



        private void barMain_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "btnAdd":
                    //ChangeStatus(false);
                    //ResetText();
                    //ClearDataBindings();
                    //ReadOnlyControl(false);

                    IsAdd = true;
                    IsEdit = false;

                    PerformAdd();
                    break;

                case "btnEdit":
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                    IsAdd = false;
                    IsEdit = true;

                    PerformEdit();
                    break;

                case "btnDelete":
                    PerformDelete();
                    break;

                case "btnSave":
                    if (IsEdit || IsAdd)
                    {
                        ChangeStatus();
                        ReadOnlyControl();
                    }

                    PerformSave();
                    break;

                case "btnCancel":
                    ChangeStatus();
                    ReadOnlyControl();

                    PerformCancel();
                    break;

                case "btnRefresh":
                    PerformRefresh();
                    break;

                case "btnSelect":
                    PerformSelectOneObject();
                    break;

                case "btnPrint":
                    PerformPrint();
                    break;

                case "btnClose":
                    Close();
                    break;

                default:
                    break;

            }
        }

        #region Virtual

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected virtual void PerformAdd() { }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected virtual void PerformEdit() { }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        protected virtual void PerformDelete() { }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected virtual void PerformSave() { }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected virtual void PerformCancel() { }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected virtual void PerformRefresh()
        {
            btnAdd.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnEdit.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnSave.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnDelete.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnRefresh.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnCancel.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            btnClose.Border = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;

        }

        /// <summary>
        /// Perform when click select button
        /// </summary>
        protected virtual void PerformSelectOneObject() { }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected virtual void PerformPrint() { }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected new virtual void ResetText() { }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected virtual void ClearDataBindings() { }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected virtual void DataBindingControl() { }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected virtual void ReadOnlyControl(bool isReadOnly = true) { }

        /// <summary>
        /// Set visibility control on form
        /// </summary>
        /// <param name="isVisit">Visibility is trule else normal</param>
        protected virtual void VisibilityControl(bool isVisit = false) { }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected virtual bool UpdateObject() { return true; }

        /// <summary>
        /// Delete object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected virtual bool InsertObject() { return true; }

        /// <summary>
        /// Load data
        /// </summary>
        protected virtual void LoadData() { }

        #endregion


        #region Method Parent

        public void SetCaptionForm(string str = "")
        {
            this.Text = str;
        }

        public string CreateID(string tbl = "", string col = "", string str = "", int num = 0, string where = "", int where_value = 0)
        {
            var result = ""; string next_str = "";
            if (where != "" && where_value != 0)
                where = " WHERE "+where+" = "+where_value+" ";
            var dt = _bllBase.SelectMax(tbl, col, where);
            string max_id = dt.Rows[0]["max_id"] + "";
            if ("" + max_id != "")
            {
                max_id = max_id.Trim();
                if (num > 0)
                    result = max_id.Substring(0, num);
                next_str = max_id.Substring(num);
                int number = Convert.ToInt32(next_str) + 1;
                if (number < 10)
                    result += "0";
                result += number;
            }
            else
                result += str + "01";
            return result;
        }

        #endregion

        private void FrmInput_Load(object sender, EventArgs e)
        {
            ReadOnlyControl();
            ChangeStatus();
            PerformRefresh();
        }

    }
}